#ifndef STATE_TEST_STATE_H
#define STATE_TEST_STATE_H
#define TEST_STATE 1

class state_TestState : public State {
public:
	// State variables.
	Scene* _scene; // A scene object pointer. Contains objects.
	Logger* _logger; // A logger object pointer. This one is used for state setup here because the engine's isn't set up yet.

	virtual void Load() {
		glEnable(GL_LIGHT0); // Turn on lights.
		glEnable(GL_LIGHT1);

		_scene = new Scene; // New scene assigned to pointer.
		_logger = new Logger("testEngine.log"); // Create the logger and assign it to the pointer.

		// Load a scene object into the scene.
		if (!_scene->AddSceneObject("Cube.mod", "./", true)) { // If the object is not created...
			_logger->Log(LOGGER_ERROR, "Cannot create new scene object: ", "./Monkey.model"); // Log an error.
		} else {
			// If there is no error, load some more objects.
			_scene->AddSceneObject("Cube.mod", "./", true);
			_scene->m_contents.objects->GetLast()->SetPosition(2.0f, 0.0f, -8.0f); // And set their positions.
			_scene->AddSceneObject("Cube.mod", "./", true);
			_scene->m_contents.objects->GetLast()->SetPosition(-3.0f, 0.0f, -12.0f);
			// We want to control the object which was created first. Position it and give it some friction.
			_scene->m_contents.objects->GetFirst()->SetPosition(0.0f, 0.0f, -8.0f);
			_scene->m_contents.objects->GetFirst()->SetFriction(0.005f);
		}
		
		_scene->Save("savedScene.aes"); // Save the scene.

		/* // This is how you load a previously saved scene.
		_scene->Load("savedScene.aes");
		*/

		// Add some cameras.
		_scene->AddCamera(new Camera(Vec3<float>(0,0,0), Vec3<float>(3,2,-8), Vec3<float>(1,1,0)), true); // We switch to this one automatically.
		_scene->AddCamera(new Camera(Vec3<float>(9,0,-7), Vec3<float>(0,0,-8), Vec3<float>(0,1,0)), false);
	}

	virtual void HandleInput(SDL_Event &event) {
		switch ( event.type )
		{
			case SDL_KEYDOWN:
				if (event.key.keysym.sym == SDLK_ESCAPE) {
					g_aspectEngine->GetCurrentState()->Close();
					g_aspectEngine->Quit(0);
				}
				if (event.key.keysym.sym == SDLK_s) {
					glShadeModel(GL_SMOOTH);
				}

				if (event.key.keysym.sym == SDLK_f) {
					glShadeModel(GL_FLAT);
				}

				if (event.key.keysym.sym == SDLK_1) {
					glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
				}

				if (event.key.keysym.sym == SDLK_2) {
					glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
				}

				if (event.key.keysym.sym == SDLK_3) {
					glPolygonMode(GL_FRONT_AND_BACK, GL_POINT);
				}
				if (event.key.keysym.sym == SDLK_DOWN) {
					_scene->m_contents.objects->GetFirst()->Drive(-0.0001f,false);
				}

				if (event.key.keysym.sym == SDLK_UP) {
					_scene->m_contents.objects->GetFirst()->Drive(0.0001f,false);
				}

				if (event.key.keysym.sym == SDLK_LEFT) {
					_scene->m_contents.objects->GetFirst()->SetSpin(0.0f, 0.1f, 0.0f);
				}

				if (event.key.keysym.sym == SDLK_RIGHT) {
					_scene->m_contents.objects->GetFirst()->SetSpin(0.0f, -0.1f, 0.0f);
				}

				if (event.key.keysym.sym == SDLK_F1) {
					_scene->m_contents.activeCamera = _scene->m_contents.cameras->GetFirst();
				}
				
				if (event.key.keysym.sym == SDLK_F2) {
					_scene->m_contents.activeCamera = _scene->m_contents.cameras->GetLast();
				}

				if (event.key.keysym.sym == SDLK_SPACE) {
					_scene->m_contents.objects->GetFirst()->SetPosition(0.0f, 0.0f, -8.0f);
					_scene->m_contents.objects->GetFirst()->SetRotation(0.0f, 0.0f, 0.0f);
					_scene->m_contents.objects->GetFirst()->SetSpin(0.0f, 0.0f, 0.0f);
					_scene->m_contents.objects->GetFirst()->SetVelocity(0.0f, 0.0f, 0.0f);
				}

				break;

			case SDL_MOUSEMOTION:
				break;
		}
	}

	virtual void Update( long lElapsedTicks ){
		_scene->Update(); // Update the scene.
	}

	virtual void Render() {
		_scene->Render(); // Render the scene.
	}

	virtual void Close()
	{
		SAFE_DELETE(_scene); // Delete the scene.
		SAFE_DELETE(_logger); // Delete the logger.
	}

};

#endif
